package ntut.andengine.game;

import javax.microedition.khronos.opengles.GL10;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.util.GLHelper;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel1 extends GameLevel {

	TextureRegion mLightTextureRegion;
	TextureRegion mPhoneTextureRegion;
	GameLevel mLevel;

	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture bitmapTexture = new BitmapTexture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		this.mLightTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "particle_point.png", 0, 0);
		this.mPhoneTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "phone.png", 32, 0);
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		
		mEngine.getTextureManager().loadTexture(bitmapTexture);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}

	@Override
	public Scene onLoadScene() {

//		String str = null;
//		
//		try {
//			
//			FileInputStream fin = this.openFileInput("levelRecord.txt");
//			
//			BufferedReader br = new BufferedReader(new InputStreamReader(fin));
//			
//			while((str = br.readLine()) != null){
//				Log.d("file", str);
//			}
//			
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			Log.d("file", e.toString());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		PhysicalFactory.CreateWall(this, new Vector2(200, 140), 400, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(280, 260), 400, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(40, 380), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(120, 500), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(40, 620), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(120, 700), 80, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(180, 500), 40, 440, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(300, 740), 40, 120, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(420, 640), 40, 160, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(300, 420), 40, 120, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(300, 580), 200, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(400, 460), 160, 40, ComponentColor.White);
        
        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(400, 400));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel1.this, GameLevel2.class);		
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
		
	// hint
		final Sprite light = new Sprite(240, 50, this.mLightTextureRegion);
		light.setScale(5);
		light.setColor(1, 0.824f, 0.234f);
		
		final Sprite phone = new Sprite(240, 30, this.mPhoneTextureRegion) {
			@Override
			protected void applyRotation(final GL10 pGL) {
				/* Disable culling so we can see the backside of this sprite. */
				GLHelper.disableCulling(pGL);

				final float rotation = this.mRotation;

				if(rotation != 0) {
					final float rotationCenterX = this.mRotationCenterX;
					final float rotationCenterY = this.mRotationCenterY;

					pGL.glTranslatef(rotationCenterX, rotationCenterY, 0);
					pGL.glRotatef(rotation, 0.5f, 1, 0);
					pGL.glRotatef(rotation, 0, 1, 0);
					pGL.glTranslatef(-rotationCenterX, -rotationCenterY, 0);

				}
			}

			@Override
			protected void drawVertices(final GL10 pGL, final Camera pCamera) {
				super.drawVertices(pGL, pCamera);

				/* Enable culling as 'normal' entities profit from culling. */
				GLHelper.enableCulling(pGL);
			}
		};
		phone.registerEntityModifier(new LoopEntityModifier(new RotationModifier(3, -40, 40)));
		
		mScene.attachChild(light);
		mScene.attachChild(phone);
	// hint
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(40, 40), ComponentColor.Red);

		this.mScene.registerUpdateHandler(this.mPhysicsWorld);		

		
		return mScene;
	}

}
